package com.bawei.myapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.myapp.adapter.CarAdapter;
import com.bawei.myapp.entity.CarEntity;
import com.bawei.myapp.utils.HttpUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车：最难的
 * 1。布局：findViewById
 * 2。网络请求查询购物车数据展示购物车列表（HttpUtils工具类）
 * 3。购物车操作
 * （1）全选和取消全选:activity
 *  (2)选择和取消选择，+，-，删除：adapter
 * 4。总价计算和数量计算：第三步中购物车的所有操作都要重新计算总价
 * 5。2件9折 3件打8折
 * 6。每满200减30，满300减50
 *
 */
public class CarActivity extends AppCompatActivity {
    private ListView lv;
    private CheckBox cbAll;
    private TextView tvMoney;
    private Button btDelete;
    private Button btAccount;
    private String url = "http://43.143.146.165:8083/car/getCars?username=zhoukun";
    private CarAdapter mCarAdapter;//适配器
    private List<CarEntity.DataBean> mDataBeanList = new ArrayList<>();//数据源



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        lv = (ListView) findViewById(R.id.lv);
        cbAll = (CheckBox) findViewById(R.id.cb_all);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        btDelete = (Button) findViewById(R.id.bt_delete);
        btAccount = (Button) findViewById(R.id.bt_account);
        //初始化列表:
        mCarAdapter = new CarAdapter(mDataBeanList,this);
        lv.setAdapter(mCarAdapter);
        //网络请求数据
        new CarTask().execute(url);
        //全选和取消全选:修改数据源(集合中全部数据select字段变成true/false)+刷新适配器
        cbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (CarEntity.DataBean dataBean : mDataBeanList) {
                    dataBean.setSelect(isChecked);
                }
                mCarAdapter.notifyDataSetChanged();
                total();
            }
        });
    }
    //总价计算+数量计算
    public void total(){
        float money = 0;//总价
        int sum = 0;//数量
        for (CarEntity.DataBean dataBean : mDataBeanList) {
            if(dataBean.isSelect()){//选中
                money+=dataBean.getCar_num()*dataBean.getGoods_price();
                sum+=dataBean.getCar_num();
            }

            //2件9折 3件打8折
//            if(sum == 2){
//                money*=0.9;
//            }else if(sum >= 3){
//                money*=0.8;
//            }
            //每满200减30，每满300减50
//            if(money >=200 && money < 300){
//                money-=30;
//            }else if(money >= 300){
//                int num = (int) (money/300);//700 num=2 900 num=3
//                money-= num * 50;
//            }


        }
        tvMoney.setText("总价：¥"+money);
        btAccount.setText("结算（"+sum+"）");
    }








    class CarTask extends AsyncTask<String,Void,String>{
        //网络请求数据
        @Override
        protected String doInBackground(String... strings) {
            return  HttpUtils.doGet(strings[0]);
        }
        //更新UI：解析json数据，将数据添加到集合中并刷新适配器
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Toast.makeText(CarActivity.this, ""+s, Toast.LENGTH_SHORT).show();
            CarEntity carEntity = new Gson().fromJson(s, CarEntity.class);
            mDataBeanList.addAll(carEntity.getData());//添加数据源
            mCarAdapter.notifyDataSetChanged();//刷新适配器
        }
    }
}